package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.SalesDao;
import cn.gson.boot.model.dao.SalesdetailsDao;
import cn.gson.boot.model.mapper.SalesDetailsMapper;
import cn.gson.boot.model.mapper.SalesMapper;
import cn.gson.boot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class SalesService {
    @Autowired
    SalesMapper salesMapper;
    @Autowired
    SalesDao salesDao;
    @Autowired
    SalesdetailsDao salesdetailsDao;

    @Autowired
    SalesDetailsMapper salesDetailsMapper;

    //销售机会分页查询
    public List<Sales> selectAllSales(String search,String type,String empName){
        return salesMapper.selectSales(search,type,empName);
    }

    //销售机会客户列表
    public List<Kehu> selectKh(String khsearch,String empName){
        return salesMapper.selectKehu(khsearch,empName);
    }

    //客户联系人查询
    public List<Contacts> selectLxr(String khName){
        return salesMapper.selectLxr(khName);
    }

  /*  //新增销售机会
    public void addSales(Sales sales){
        sales.setSalesUpdatetime(new Date());
        salesDao.save(sales);
    }*/

    //新增销售机会和销售详情
    public void addSales(Sales sales,List detailss){
            JSONArray json = JSONArray.parseArray(String.valueOf(detailss));
            //JSONObject jsonObject = JSONObject.parseObject(String.valueOf(json));
            sales.setSalesUpdatetime(new Date());
            sales.setSalesSc(0);
            salesDao.save(sales);
            salesdetailsDao.deleteAll(salesDetailsMapper.Salesxq(sales.getSalesId()));
            Salesdetails salesdetails = null;
            JSONObject ob = null;
            Productt productt = null;
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer detailsId = ob.getInteger("deteailsId");
                Integer producttId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(producttId);
                salesdetails = new Salesdetails();
                if(detailsId!=null) {
                    salesdetails.setDetailsId(detailsId);
                }
                salesdetails.setProductt(productt);
                salesdetails.setSproductNum(purNumber);
                salesdetails.setSproductSubtotal(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                salesdetails.setSales(sales);
                salesdetailsDao.save(salesdetails);
            }
    }

    //根据销售id删除销售机会
    public void delSales(Integer salessId){
        Sales sales = salesDao.findById(salessId).get();
        sales.setSalesSc(1);
        salesDao.save(sales);
    }

    //查询要修改的销售机会
    public List<Sales> Selectsalest(Integer salessId){
        return salesMapper.selectSalesId(salessId);
    }

    //查询需要修改的销售详情
    public List<Salesdetails> Salesxq(Integer salessId){
        return salesDetailsMapper.Salesxq(salessId);
    }

    //查询签了合同的销售机会不能被删除
    public Hetong selectHt(Integer salesId) {
        return salesMapper.selectHt(salesId);
    }

    //查询客户的来源
    public List<SalesVo> selectLaiy(String salesMan) {
        return salesMapper.selectLaiy(salesMan);
    }

    //查询机会的来源(统计图)
    public List<SalesVo> selectPossibility(String salesMan) {
        return salesMapper.selectPossibility(salesMan);
    }

    //销售阶段的阶段数量(漏斗)
    public List<SalesVo> stageNum(String salesMan) {
        return salesMapper.stageNum(salesMan);
    }

    //销售阶段金额
    public List<SalesVo> stageMoney(String salesMan) {
        return salesMapper.stageMoney(salesMan);
    }

    //销售机会预期签单时间(漏斗)
    public List<SalesVo> selectsDate(String salesMan) {
        return salesMapper.selectDate(salesMan);
    }

    //销售漏斗选择框
    public List<User> selectUser() {
        return salesMapper.selectUser();
    }

    public List<SalesVo> selectsDateMonth(String salesMan) {
        return salesMapper.selectsDateMonth(salesMan);
    }
}
